package com.china08.yxyapi.api.comm.upload;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.util.YXCSPUtil;

@CrossOrigin
@RestController("UploadImgController")
public class UploadImgController {
    @Autowired
    private YXCSPUtil yxcspUtil;

    /**
     * 上传图片
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/comm/uploadImg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel uploadImage(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        RespModel respModel = new RespModel();
        Assert.isTrue(file.getSize() != 0, "文件不能为空!");
        Assert.isTrue(file.getSize() < 2 * 1024 * 1024, "文件不能大于2mb!");
        String url = yxcspUtil.push(file);
        respModel.url = url;
        respModel.setMessage("上传成功");
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
